﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Data;

namespace Bzure.Apps.Security.DataAccess
{
    public class UserRoleMapRepository : RepositoryBase
    {
        static UserRoleMapRepository()
        {
            UserRoleMap entity = new UserRoleMap();
            Type type = entity.GetType();
            AppendDataMap(type, "UserID", () => entity.UserID);
            AppendDataMap(type, "RoleID", () => entity.RoleID);
            ResultMapDelegate<UserRoleMap> queryMap = UsersInRoles_Map;
            QueryMapContainer[entity.GetType()] = queryMap;
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="contractProvider"></param>
        public UserRoleMapRepository(Bzure.Common.Transactions.ITransactionSupport contractProvider)
            : base(contractProvider)
        {

        }
        /// <summary>
        /// 实体对应数据库中的表
        /// </summary>
        public override string TableName
        {
            get
            {
                return "T_Security_UserInRoles";
            }
        }
        /// <summary>
        /// 角色实体数据映射
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private static UserRoleMap UsersInRoles_Map(DataReader reader)
        {
            UserRoleMap usersinroles = new UserRoleMap()
            {
                UserID = reader.GetString("UserID"),
                RoleID = reader.GetString("RoleID")
            };
            return usersinroles;
        }
        public string[] GetTeamRoleIDsByUser(string UserID)
        {
            string sql = string.Format("SELECT RoleID FROM  {0} WHERE UserID=@UserID", TableName);
            return new SqlEntityMap<string>(this.ContractProvider, sql)
              .AddString("UserID", UserID)
              .SetMap(_StringMap)
           .AsList().ToArray();
        }

        static string _StringMap(DataReader reader)
        {
            return reader.GetString("RoleID");
        }
    }
}
